<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> 
        <a href="#" class="tip-bottom" data-original-title="Go to Home"><i class="icon-home"></i> 首页</a> 
        <a href="#" class="tip-bottom" data-original-title="">配置管理</a> 
        <a href="#" class="current">粉丝配置</a> 
    </div>
    
   <!--  <h1>Tables</h1> -->
  </div>
  <div class="container-fluid">
   <!--  <hr> -->
    <div class="row-fluid">
      <div class="span12">
        <div class="row-fluid">
            <button id="btnadd" class="btn btn-success pull-right">添&nbsp;加</button>
        </div>
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>粉丝配置</h5>
          </div>
          <div class="widget-content nopadding">
            <table id="dataList" class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>粉丝等级</th>
                  <th>等级名称</th>
                  <th>最低积分</th>
                  <th>最高积分</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                
              </tbody>
            </table>
          </div>
        </div>

        <div class="row-fluid">
            <div id="pagination" class="pagination alternate pull-right">
              
            </div>
        </div>

        </div>
      </div>
    </div>
  </div>
</div>


<div id="dialog-form" title="Create new user" style=" display: none;">
  <p class="validateTips"></p>

   <form id="submitform" action="#" method="get" class="form-horizontal">
      <input type="hidden" id="vipid">      
      <div class="control-group">
        <label class="control-label">粉丝等级 :</label>
        <div class="controls">
          <input id="level" type="text" class="span11" placeholder="">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label">等级名称 :</label>
        <div class="controls">
          <input id="name" type="text" class="span11" placeholder="">
        </div>
      </div>
      
      <div class="control-group">
        <label class="control-label">最低积分 :</label>
        <div class="controls">
          <input id="creditCeil" type="text" class="span11" placeholder="">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label">最高积分 :</label>
        <div class="controls">
          <input id="creditFloor" type="text" class="span11">
          <span class="help-block"></span>
        </div>
      </div>
      
      
    </form>

</div>

<script type="text/javascript">

  $(function(){
    getDataList()
  });

/**
* 初始化为空
*/
function Initialization(){

  var submitform=document.getElementById('submitform');
    
    submitform.level.value='';
    submitform.name.value='';
    submitform.creditCeil.value='';
    submitform.creditFloor.value='';
}

 var btnadd=document.getElementById('btnadd');
 utils.addEvent(btnadd,'click',addData);

 function addData(){
    $( "#dialog-form" ).dialog({
      width: 600,
      title:'添加主播等级信息',
      modal:true,
      closeOnEscape:true,
      resizable:false,
      open:function(res){
          Initialization();
        },
      buttons:{
        "添加":function(){

          var submitform=document.getElementById('submitform'),
          data={};

          data.action="add"
          data.level=submitform.level.value;
          data.name=submitform.name.value;
          data.lower=submitform.creditCeil.value;
          data.higher=submitform.creditFloor.value;

          $.ajax({
              type:"post",
              url:"/configmgr/updateFans",
              data:data,
              dataType:'json',
              success:function(res){
                if(res.code==0){
                    getDataList();
                    $("#dialog-form" ).dialog("close");
                }else{
                    alert(res.info);
                }
              }
          });

          
        },
        "取消":function(){
          $(this).dialog("close");
        }

      }
  });
 }

// get vip data
function getDataList(index){
  var data={};
  data.page=index||0;
  data.numPerPage=pagesize;
  $.ajax({
    type:"post",
    url:"/configmgr/getFans",
    data:data,
    dataType:'json',   
    success:function(res){
      
      var data=res.data;
      var table=document.getElementById("dataList");
      var tbody= table.getElementsByTagName('tbody')[0];

      dataTable.clearData(tbody);
      
      for(var i=0;i<data.list.length;i++){
        addRow(tbody,'',data.list[i]);
      }
      
      if(!index||index==0){
        initPagination(data.count);
      }

    }
  });
}

function initPagination(page) {
  $("#pagination").pagination(page, {
      callback: pageselectCallback,
      items_per_page:pagesize, // 每页只显示一条记录
      num_display_entries : 3,
      num_edge_entries : 1, 
      prev_text : "上一页",  
      next_text : "下一页", 
  });
}

function pageselectCallback(page_index, jq){
    getDataList(page_index);
    return false;
}

function addRow(table,index,data){
  var row=table.insertRow();
  var cell=row.insertCell(0);
  var cell1=row.insertCell(1);
  var cell2=row.insertCell(2);
  var cell3=row.insertCell(3);
  var cell4=row.insertCell(4);
    
  //cell1.innerHTML=index;
  cell.innerHTML=data.level;
  cell1.innerHTML=data.name;
  cell2.innerHTML=data.lower;
  cell3.innerHTML=data.higher;

  var deit=document.createElement("a"),
  del=document.createElement("a");
  
  deit.innerHTML='编辑';
  deit.className="table-edit";
  deit.data=data.id;
  utils.addEvent(deit,'click',dataEdit);

  del.innerHTML='删除';
  del.data=data.id; 
  utils.addEvent(del,'click',dataDelete); 

  cell4.appendChild(deit);
  cell4.appendChild(del);
    
  return row;
}

function dataEdit(){
    var btn=this;
    var url="/configmgr/updateFans/"+btn.data;
  
   $( "#dialog-form" ).dialog({
        width: 600,
        title:'修改粉丝等级信息',
        modal:true,
        closeOnEscape:true,
        resizable:false,
        open:function(){
          var data={};
          data.action='';

          Initialization();

          $.ajax({
            type:"post",
            data:data,
            url:url,
            dataType:'json',
            success:function(res){
              var data= res.data;

              var submitform=document.getElementById('submitform');
              submitform.level.value=data.level;
              submitform.name.value=data.name;
              submitform.creditCeil.value=data.lower;
              submitform.creditFloor.value=data.higher;              
            }
          });
        },
        buttons:{
          "修改":function(){

            var submitform=document.getElementById('submitform'),
            data={};

            data.action="update";
            data.level=submitform.level.value;
            data.name=submitform.name.value;
            data.lower=submitform.creditCeil.value;
            data.higher=submitform.creditFloor.value;

            $.ajax({
              type:"post",
              data:data,
              url:url,
              dataType:'json',
              success:function(res){
                if(res.code==0){
                    getDataList();
                    $("#dialog-form" ).dialog("close");
                }
                else{
                    alert(res.info);
                }
              }
            });

          },
          "取消":function(){
            $(this).dialog("close");
          }

        }
   });

  

}

function dataDelete(){
  if(confirm("确定要删除该数据")){
    var btn=this;
    var url="/configmgr/updateFans/"+btn.data,
    data={};
    data.action="del";
    $.ajax({
      type:"post",
      data:data,
      url:url,
      dataType:'json',
      success:function(res){
        if(res.code==0){
            getDataList();
        }
        else{
          alert(res.info);
        }     
      }
    });
  }
}


</script>
